package com.cy.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 验证码工具类
 */
@Configuration
public class KaptchaConfig {

    @Bean
    public Producer kaptchaProducer() {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "100");   //设置验证码宽度
        properties.setProperty("kaptcha.image.height", "40");   //设置验证码高度
        properties.setProperty("kaptcha.textproducer.font.size", "32");     //设置字体大小
        properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");     //设置字体颜色
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ");    //设置随机字符范围
        properties.setProperty("kaptcha.textproducer.char.length", "4");        //设置随机字符个数
        //设置噪声,也就是干扰,因为kaptch自带干扰,所以这里我们不加干扰了
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");

        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;
    }

}
